//
//  PPSettingBirthdayDatePicker.m
//  pickpic
//
//  Created by GaoYongyue on 16/10/1.
//  Copyright © 2016年 MicroDream. All rights reserved.
//

#import "PPSettingBirthdayDatePicker.h"

@interface PPSettingBirthdayDatePicker ()
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (nonatomic, strong) DGPopoverView *popoverView;
@property (nonatomic, strong) UIView *aView;

@end
@implementation PPSettingBirthdayDatePicker

- (void)awakeFromNib
{
    [super awakeFromNib];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy年M月d日";
    NSDate *minDate = [dateFormatter dateFromString:@"1900年1月1日"];
    _datePicker.minimumDate = minDate;
    _datePicker.maximumDate = [NSDate date];
}

- (void)setSelectedDateString:(NSString *)selectedDateString
{
    _selectedDateString = selectedDateString;

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy年M月d日";
    NSDate *date = [dateFormatter dateFromString:selectedDateString];
    [_datePicker setDate:date animated:NO];
}

- (IBAction)cancleButtonDidPress:(id)sender {
    [_popoverView dismissAnimated:YES completion:nil];
}

- (IBAction)confirmButtonDidPress:(id)sender {
    __weak typeof(self)weakSelf = self;
    NSDate *selectedDate = _datePicker.date;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy年M月d日";
    NSString *dateString = [dateFormatter stringFromDate:selectedDate];

    [_popoverView dismissAnimated:YES completion:^{
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:weakSelf.aView animated:YES];
        [PPNetworkService updateProfileWithName:nil gender:nil country:nil state:nil city:nil birthday:dateString signature:nil type:4 callback:^(id data, NSError *error) {
            [hud hide:YES];
            if (data)
            {
                [weakSelf.aView toastMessage:@"变更成功" duration:1.f];
                if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(didUpdateBirthdaySuccessfull:)])
                {
                    [weakSelf.delegate didUpdateBirthdaySuccessfull:dateString];
                }
            }
            else
            {
                [weakSelf.aView toastMessage:error.networkPrompt duration:1.f];
            }
        }];
    }];
}

- (void)showInView:(UIView *)view
{
    _aView = view;
    self.width = view.width;
    self.bottom = view.height;

    _popoverView = [[DGPopoverView alloc] initWithContainerView:view contentView:self options:DGPopoverViewAnimationOptionUpward backgroundStyle:DGPopoverViewBackgroundStyleDimmed];
    [_popoverView displayAnimated:YES completion:nil];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
